package ru.niir.goodfil.ui;

import ru.niir.goodfil.db.GoodType;
import ru.niir.goodfil.utils.SumFormat;

import javax.faces.context.FacesContext;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Волков Олег on 29.10.14.
 */
public class GoodTypeAbsenceReportBean extends PageBean {


    private int rowsCount = 0;

    private Date beginDate;

    private Date endDate;

    private List<GoodType> goodTypes;

    public GoodTypeAbsenceReportBean() {
        init();
    }

    public void init(){
        Map sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        GoodAbsenceReportBean reportBean = (GoodAbsenceReportBean)sessionMap.get("GoodAbsenceReportBean");
        if(reportBean != null){
            beginDate = reportBean.getBeginDate();
            endDate = reportBean.getEndDate();
        }
        goodTypes = getGoodTypeAbsence();
        rowsCount = goodTypes.size();
    }

    /**
     * Метод устанавливает количество товара, которого нехватило (на категорию)
     * @return коллекция категорий товаров
     */
    public List<GoodType> getGoodTypeAbsence(){
        List<GoodType> result = gf().getGoodTypesByParentId(33);
        for(GoodType goodType : result){
            goodType.setAbsence(gf().getAbsenseByGoodTypeId(goodType.getId(), beginDate, endDate));
            goodType.setAbsenceCost(gf().getAbsenceCostByGoodTypeId(goodType.getId(), beginDate, endDate));
        }
        return result;
    }

    public int getRowsCount() {
        return rowsCount;
    }

    public String getBeginDate() {
        SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
        return format.format(beginDate);
    }

    public String getEndDate() {
        SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
        return format.format(endDate);
    }

    public String getTotalAbsenceCost() {
        double totalAbsenceCost = 0;
        for(GoodType goodType : goodTypes){
            totalAbsenceCost+= goodType.getAbsenceCost();
        }
        return SumFormat.formatSum("###,###,##0.00", totalAbsenceCost);
    }


    public List<GoodType> getGoodTypes() {
        return goodTypes;
    }
}
